/* 
 * Submit form using Ajax
 * - action: Action to be invoked
 * - form: Form to be submitted
 * - error: where error data returned from the action is displayed
 * - success: where success data returned from the action is displayed
 * - success_js: javascript to be invoked when form is submitted successfully
 * - chainAction: invokes a full page action request after the ajax form 
 * 		submission has completed successfully
 */
var ajaxSubmitForm = function(event) {
	event.preventDefault();
	$.post(event.data.action,
		$(event.data.form).serialize(),
		function(data) {
			if($('.ajax-status', data).html() == 'ALERT') {
				if(event.data.error) {
					$(event.data.error).html(data);
					/* display only the first error */
					$('li', event.data.error).first().css('display','list-item');
					$(event.data.error).slideDown();
				}				
			} else {
				if(event.data.success) {					
					if(event.data.success_js) {
						event.data.success_js(event.data.success, data);
					} else {					
						$(event.data.success).fadeOut(function(){
							$(this).html(data).fadeIn('slow');
						});
					}
				}
				if(event.data.chainAction) {
					location.href=event.data.chainAction;
				}
			}
	});
};

/* 
 * Submit form
 * - action: Action to be invoked
 * - form: Form to be submitted
 */
var submitForm = function(event) {
	event.preventDefault();
	location.href=event.data.action+'?'+$(event.data.form).serialize();
};

/* Form reset */
var formReset = function(event) {
	$(event.data.form)[0].reset();
	$(event.data.error).slideUp();
};

var hyperLink = function(event) {
	event.preventDefault();
	if(event.data.result != null) {
		$.post(event.data.action,
			function(data) {
				$(event.data.result).html(data);
			});
	} else {
		location.href = event.data.action;
	}	
};

/* Prevents user from selecting invalid dates
 * The data inputs are represented by 3 select tags with 
 * name='month', name='day', and name='year' */
var dateValidation = function() {
	var day = $('select[name="day"] option:selected',$(this).parent()).val();
	if(day > 28) {
		var month = $('select[name="month"] option:selected',$(this).parent()).val();
		if((month <= 7 && month%2 == 0) || (month >= 8 && month%2 != 0)) {
			if(month == 2) {
				var year = $('select[name="year"] option:selected',$(this).parent()).val();
				if(year == -1 || year%4 == 0) {
					/* Should not exceed 29 days */
					if(day > 29) {
						if($(this).attr('name') != 'day') {
							$('select[name="day"]',$(this).parent()).val('-1');
						} else {
							$('select[name="month"]',$(this).parent()).val('-1');
						}
					}
				} else {
					/* Should not exceed 28 days */
					if(day > 28) {
						if($(this).attr('name') != 'day') {
							$('select[name="day"]',$(this).parent()).val('-1');
						} else {
							$('select[name="month"]',$(this).parent()).val('-1');
						}
					}
				}
			} else {
				/* Should not exceed 30 days */
				if(day > 30) {
					if($(this).attr('name') != 'day') {
						$('select[name="day"]',$(this).parent()).val('-1');
					} else {
						$('select[name="month"]',$(this).parent()).val('-1');
					}
				}
			}
		}
	}
};

var showTooltip = function(event) {
	var target = $(event.target);
	var html = target.next('span').html();
	$('div.tooltip').remove();
	$('<div class="tooltip ui-widget-header ui-corner-all">'+html+'</div>').appendTo('body');
	var offset = target.offset();
	var tooltipX = offset.left + target.width() + 35;
	var tooltipY = offset.top;
	$('div.tooltip').css({top: tooltipY, left: tooltipX});
	$('div.tooltip').animate({
	    left: '-=20'
	  }, 'fast', 'swing');
};

var hideTooltip = function(event) {
	$('div.tooltip').remove();
};

function resize(targetDiv, targetContent) {
	var newdiv = document.createElement('div');
	newdiv.innerHTML = targetContent;
	$(newdiv).css('visibility','hidden');
	var currentWidth = $(targetDiv).width();
	var currentHeight = $(targetDiv).height();
	$(targetDiv).html(newdiv);
	var newWidth = $(targetDiv).width();
	var newHeight = $(targetDiv).height();
	$(targetDiv).width(currentWidth);
	$(targetDiv).height(currentHeight);
	$(targetDiv).queue(function() {
		$(this).animate({width:newWidth,height:newHeight});
		$(this).dequeue();
	});
	$(targetDiv).queue(function() {
		$(targetDiv +' div').css('visibility','visible');
		$(this).dequeue();
	});
}

$(document).ready(function() {
	/*
	$('#menu-bar a').css('cursor','pointer');
	$('#menu-bar .hlist li').first().css('border-left','none');
	*/
	
	/* overlay */
	/*
	$("a[rel]").overlay({
		onBeforeLoad: function() {
			// grab wrapper element inside content
			var wrap = this.getOverlay().find(".contentWrap");
			// load the page specified in the trigger
			wrap.load(this.getTrigger().attr("href"));
		}
	});
	*/
});